package com.digital.smartgrow.controller.sysinfo;

import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.entity.sysinfo.DictionaryItem;
import com.digital.smartgrow.service.sysinfo.DictionaryItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 字典项 前端控制器
 * </p>
 *
 * @author sunyuechao
 * @since 2022-04-23
 * 修改
 */
@Api(tags = "字典项")
@RestController
@RequestMapping("${context.prefix}/dictItem")
public class DictionaryItemController {

    @Autowired
    private DictionaryItemService dictionaryItemService;

//    @ApiOperation(value = "列表分页查询")
//    @PostMapping("/getListByPage")
//    public Object getListByPage(@RequestBody DictionaryItemPageDTO dictionaryItemPageDTO){
//        return dictionaryItemService.getListByPage(dictionaryItemPageDTO);
//    }

    @ApiOperation(value = "字典项详情")
    @GetMapping("/{id}")
    public Object getInfo(@PathVariable Integer id){
        if (id == null) return "id不能为空";
        return dictionaryItemService.getInfo(id);
    }

    @ApiOperation(value = "字典项-下拉列表")
    @GetMapping("/list")
    public Object getList(String dictNumber){
        return dictionaryItemService.getList(dictNumber);
    }

    @ApiOperation(value = "字典项-删除")
    @DeleteMapping("/{id}")
    public Object removeById(@PathVariable Integer id) {
        if (id == null) return Result.fail("id不能为空");
        DictionaryItem dictionary = dictionaryItemService.getById(id);
        if(dictionary.getIssys()==1){
            return Result.fail("系统字典不允许删除");
        }
        return dictionaryItemService.removeById(id);
    }

    @ApiOperation(value = "字典项-新增")
    @PostMapping()
    public Object add(@Valid @RequestBody DictionaryItem dictionary) {
        return dictionaryItemService.save(dictionary);
    }

    @ApiOperation(value = "字典项-编辑")
    @PutMapping()
    public Object edit(@Valid @RequestBody DictionaryItem dictionary) {

        if (dictionary == null || dictionary.getId() == null) return Result.fail("id不能为空");
        if(dictionary.getIssys()==1){
            return Result.fail("系统字典不允许编辑");
        }
        return dictionaryItemService.updateById(dictionary);
    }
}
